﻿<cfsilent>
	<cfscript>
		
		usageAdvice = getProperty("serviceFactory").getBean("classroomUsageAdvice");
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		response = structNew();
		
		transaction {
			
			sql = "	SELECT
						c.cr_id,c.cr_usage
					FROM
						t_classroom c
					WHERE
						c.cr_id = :classroomId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "classroomId", value = event.getArg("CRID"), cfsqltype = "cf_sql_char");
			rs_clsroomEntity = queryObj.execute(sql = sql).getResult();
			
			usageAdvice.parseUsage( rs_clsroomEntity.cr_usage );
			
			switch ( event.getArg('prop') ) {
				
				/* 正向检测 只要教室设为禁用 则任何用途都非法 */
				case "Running":
					
					if ( yesNoFormat(event.getArg('status')) ) {
						usageAdvice.setRunning( true );
					}
					else {
						usageAdvice.setRunning( false );
					}
					
					if ( not usageAdvice.getRunning() ) {
						usageAdvice.setForCourse( false );
						usageAdvice.setForExam( false );
						usageAdvice.setForBorrow( false );
						usageAdvice.setForStudy( false );
					}
					
					break;
				
				/* 反向检测 允许排课、排考等用途的教室 是否目前还是禁用 如果是禁用状态 则取消禁用标志 */
				case "ForCourse":
					
					if ( yesNoFormat(event.getArg('status')) ) {
						usageAdvice.setForCourse( true );
					}
					else {
						usageAdvice.setForCourse( false );
					}
					
					if ( usageAdvice.getForCourse() ) {
						usageAdvice.setRunning( true );
					}
					
					break;
				
				case "ForExam":
					
					if ( yesNoFormat(event.getArg('status')) ) {
						usageAdvice.setForExam( true );
					}
					else {
						usageAdvice.setForExam( false );
					}
					
					if ( usageAdvice.getForExam() ) {
						usageAdvice.setRunning( true );
					}
				
					break;
				
				case "ForBorrow":
					
					if ( yesNoFormat(event.getArg('status')) ) {
						usageAdvice.setForBorrow( true );
					}
					else {
						usageAdvice.setForBorrow( false );
					}
					
					if ( usageAdvice.getForBorrow() ) {
						usageAdvice.setRunning( true );
					}
					
					break;
				
				case "ForStudy":
					
					if ( yesNoFormat(event.getArg('status')) ) {
						usageAdvice.setForStudy( true );
					}
					else {
						usageAdvice.setForStudy( false );
					}
					
					if ( usageAdvice.getForStudy() ) {
						usageAdvice.setRunning( true );
					}
					
					break;
				
			}

			sql = "	UPDATE
						t_classroom c
					SET
						c.cr_usage = :usage 
					WHERE
						c.cr_id = :classroomId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "classroomId", value = rs_clsroomEntity.cr_id, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "usage", value = usageAdvice.stringUsage(), cfsqltype = "cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			structInsert(response, "Running", usageAdvice.getRunning());
			structInsert(response, "ForCourse", usageAdvice.getForCourse());
			structInsert(response, "ForExam", usageAdvice.getForExam());
			structInsert(response, "ForBorrow", usageAdvice.getForBorrow());
			structInsert(response, "ForStudy", usageAdvice.getForStudy());
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>